import axios from "axios";
import type {
  AxiosInstance,
  AxiosError,
  AxiosRequestConfig,
  AxiosResponse,
} from "axios";
import { showToast } from "../utils/toast";
// import { useUserStore } from "@/store/modules/user";
import { ResultEnum } from "../constants/httpEnums";
import { ResultData } from "./type";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { navigationRef,replaceCurrentScreen,navigate } from '../routes/app.routes';


// import { LOGIN_URL } from "@/config/config";
// import { RESEETSTORE } from "../reset";
// import router from "@/router";
// import { initMock } from "@/mock/mock";


export const service: AxiosInstance = axios.create({
  // 判断环境设置不同的baseURL
  baseURL: "https://www.xiaot.club/api/",
  timeout: ResultEnum.TIMEOUT as number,
});

/**
 * @description: 请求拦截器
 * @returns {*}
 */
service.interceptors.request.use(
  async (config) => {
    // const userStore = useUserStore();
    // const token = userStore.token;
    const token = await AsyncStorage.getItem("TOKEN");
    if (token) {
      config.headers.token = token;
    }
    return config;
  },
  (error: AxiosError) => {
    showToast("error", "出错了", error.message);
    return Promise.reject(error);
  }
);
/**
 * @description: 响应拦截器
 * @returns {*}
 */
service.interceptors.response.use(
  (response: AxiosResponse) => {
    const { data } = response;

    // console.log("response---------", response,'data:',data)

    // * 登陆失效（code == 203）
    if (data.code === ResultEnum.EXPIRE) {
      // replaceCurrentScreen('Login',{})
      showToast("error", "出错了", data.message || ResultEnum.ERRMESSAGE);
      // router.replace(LOGIN_URL);
      return Promise.reject(data);
    }

    if (data.code && data.code !== ResultEnum.SUCCESS) {
      showToast("error", "出错了", data.msg || ResultEnum.ERRMESSAGE);
      return Promise.reject(data);
    }
    return data;
  },
  (error: AxiosError) => {
    // 处理 HTTP 网络错误
    let message = "";
    // HTTP 状态码
    const status = error.response?.status;
    console.log("err---------", error)

    switch (status) {
      case 401:
        message = "token 失效，请重新登录";
        break;
      case 403:
        message = "拒绝访问";
        break;
      case 404:
        message = "请求地址错误";
        break;
      case 500:
        message = "服务器故障";
        break;
      default:
        message = "网络连接故障";
    }

    showToast("error", "出错了", message);
    return Promise.reject(error);
  }
);

/**
 * @description: 导出封装的请求方法
 * @returns {*}
 */
const http = {
  get<T>(
    url: string,
    params?: object,
    config?: AxiosRequestConfig
  ): Promise<ResultData<T>> {

    // console.log('---get--url-',url,'params:',params)

    return service.get(url, { params, ...config });
  },

  post<T>(
    url: string,
    data?: object,
    config?: AxiosRequestConfig
  ): Promise<ResultData<T>> {
    return service.post(url, data, config);
  },

  put<T>(
    url: string,
    data?: object,
    config?: AxiosRequestConfig
  ): Promise<ResultData<T>> {
    return service.put(url, data, config);
  },

  delete<T>(
    url: string,
    data?: object,
    config?: AxiosRequestConfig
  ): Promise<ResultData<T>> {
    return service.delete(url, { data, ...config });
  },
};

export default http;
